package com.api.shopapi.service.impl;

import com.api.shopapi.entity.ArticleClass;
import com.api.shopapi.entity.dto.ArticleClassDTO;
import com.api.shopapi.mapper.ArticleClassMapper;
import com.api.shopapi.service.ArticleClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ArticleClassServiceImplOne")
public class ArticleClassServiceImpl implements ArticleClassService {


    @Autowired
    private ArticleClassMapper articleClassMapper;

    @Override
    public int insert(ArticleClass articleClass) {
        return articleClassMapper.insert(articleClass);
    }

    @Override
    public int update(ArticleClass articleClass) {
        return articleClassMapper.update(articleClass);
    }

    @Override
    public ArticleClass getByArticleClassCode(String articleClassCode) {
        return articleClassMapper.getByArticleClassCode(articleClassCode);
    }

    @Override
    public List<ArticleClass> getListByParentArticleClassCode(String parentArticleClassCode) {
        return articleClassMapper.getListByParentArticleClassCode(parentArticleClassCode);
    }

    @Override
    public List<ArticleClassDTO> getListByGovernmentCode(String governmentCode) {
        List<ArticleClassDTO> articleClassDTOList = articleClassMapper.getFirstClassList(governmentCode);
        for (ArticleClassDTO articleClassDTO : articleClassDTOList) {
            List<ArticleClass> articleClassList = articleClassMapper.getListByParentArticleClassCode(articleClassDTO.getArticleClassCode());
            articleClassDTO.setArticleClassList(articleClassList);
        }
        return articleClassDTOList;
    }

    @Override
    public PageInfo<ArticleClassDTO> getPage(int pageIndex, int pageSize, String name, String governmentCode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ArticleClassDTO> articleClassDTOList = articleClassMapper.getPage(name, governmentCode);
        for (ArticleClassDTO articleClassDTO : articleClassDTOList) {
            List<ArticleClass> articleClassList = articleClassMapper.getAllListByParentArticleClassCode(articleClassDTO.getArticleClassCode());
            articleClassDTO.setArticleClassList(articleClassList);
        }
        return new PageInfo<>(articleClassDTOList);
    }



}
